/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.edmo.fastener.dao;

import com.edmo.fastener.domain.CustomerOrder;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author obelog
 */

@Repository
@Component
@Scope("prototype")
public class CustomerOrderDao {
    
    @PersistenceContext
    private EntityManager entityManager;
    
    public CustomerOrder find(Long id) {
        return entityManager.find(CustomerOrder.class, id);
    }
    
    @SuppressWarnings("unchecked")
    public List<CustomerOrder> getOrders() {
        return entityManager.createQuery("select p from CustomerOrder p").getResultList();
    }
    
    @Transactional
    public CustomerOrder save(CustomerOrder customerOrder) {
        if (customerOrder.getId() == null) {
            entityManager.persist(customerOrder);
            return customerOrder;
        } else {
            return entityManager.merge(customerOrder);
        }
    }
    
}
